package com.cetool.autocms.service.sys.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cetool.autocms.common.PageRequest;
import com.cetool.autocms.enumeration.OperateEnum;
import com.cetool.autocms.mapper.sys.MenuMapper;
import com.cetool.autocms.pojo.domain.sys.TopMenuListDO;
import com.cetool.autocms.pojo.dto.sys.MenuInfoDTO;
import com.cetool.autocms.pojo.dto.sys.TopMenuListDTO;
import com.cetool.autocms.pojo.param.sys.BatchDeleteMenuParam;
import com.cetool.autocms.pojo.param.sys.QueryMenuListParam;
import com.cetool.autocms.pojo.param.sys.SaveMenuParam;
import com.cetool.autocms.pojo.request.sys.BatchDeleteMenuRequest;
import com.cetool.autocms.pojo.request.sys.QueryMenuListRequest;
import com.cetool.autocms.pojo.request.sys.SaveMenuRequest;
import com.cetool.autocms.service.sys.MenuService;
import com.cetool.autocms.util.BeansUtil;
import com.github.pagehelper.PageSerializable;
import com.github.pagehelper.page.PageMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 菜单管理服务实现层
 *
 * @author 戴越
 * @date 2021/11/24
 */
@Service
@Slf4j
public class MenuServiceImpl implements MenuService {
    /**
     * 注入菜单管理持久层
     */
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public PageSerializable<MenuInfoDTO> listPageMenuInfo(PageRequest<QueryMenuListRequest> request) {
        QueryMenuListParam param = new QueryMenuListParam();
        BeanUtil.copyProperties(request.getData(), param);
        PageMethod.startPage(request.getPageNum(), request.getPageSize());
        return BeansUtil.pageCopy(new PageSerializable<>(menuMapper.getMenuInfoList(param)), MenuInfoDTO.class);
    }

    @Override
    public OperateEnum saveMenu(SaveMenuRequest request) {
        SaveMenuParam param = new SaveMenuParam();
        BeanUtil.copyProperties(request, param);
        menuMapper.saveMenu(param);
        return OperateEnum.ADD_SUCCESS;
    }

    @Override
    public List<TopMenuListDTO> queryTopMenuList() {
        List<TopMenuListDO> topMenuList = menuMapper.queryTopMenuList();
        TopMenuListDO top = new TopMenuListDO();
        top.setId(-1);
        top.setMenuName("-请选择-");
        topMenuList.add(0, top);
        return BeansUtil.listCopy(topMenuList, TopMenuListDTO.class);
    }

    @Override
    public OperateEnum batchDeleteMenu(BatchDeleteMenuRequest request) {
        BatchDeleteMenuParam param = new BatchDeleteMenuParam();
        BeanUtil.copyProperties(request, param);
        menuMapper.batchDeleteMenu(param);
        return OperateEnum.DELETE_SUCCESS;
    }
}
